package xq.gwt.mvc.model;

public class StringPropertyModel extends AbstractPropertyModel {
		
	private static final long serialVersionUID = -6867050767776033557L;
	private String value;
	private String regEx;

	public String getValue() {
		return value;
	}

	public void setValue(String value) {		
		setHasError(!validateText(value));
		String oldText = this.value;
		this.value = value;
		notifyPropertyChanged(oldText, value);
	}

	@Override
	public String getText() {		
		return getValue();
	}

	@Override
	public void setText(String text) {		
		setValue(text);		
	}
	
	private boolean validateText(String text){
		if(regEx == null)
			return true;
		if(text == null)
			return true;
		return text.matches(regEx);		
	}
	
	public Object getObjectValue() {		
		return getValue();
	}

	public Class getPropertyType() {		
		return String.class;
	}

	public String getRegEx() {
		return regEx;
	}

	public void setRegEx(String regEx) {
		this.regEx = regEx;
	}
	
	
}
